package business.database.newcorps;

import java.util.List;

import org.apache.log4j.Logger;
import business.ViewLogger;
import business.coding.CandidateCenter;
import business.database.Database;
import dao.Center

/**
 * Acceso a la tabla de precodecorp
 * @author 53385255R
 *
 */
public class PreCodeCorpDAO {
	
	private Logger _log;
	
	public PreCodeCorpDAO() {
		_log = Logger.getLogger(PreCodeCorpDAO.class);
	}
	/**
	* Crea la tabla PreCodeCorp para almacenar los centros candidatos
	*/
   public void createPreCodeCorp() {
	   //se crea la tabla de direcciones codificadas si no existe
	   try {
	   Database.getInstance().sqlnewcorps.execute("create table PreCodeCorp (idcodecorp integer, corp varchar(255), " +
		   "ISO varchar(5)," +
		   "PROVINCIA varchar(2)," +
		   "INSTIT varchar(4)," +
		   "ORGANISMO varchar(6)," +
		   "DPTO varchar(8)," +
		   "SECTOR varchar(8)," +
		   "context double," +
		   "permatch double," +
		   "autoCod integer)")
	   } catch (Exception e) {
		   _log.error ("No se ha podido crear la tabla PreCodeCorp. Probablemente ya exista.",e);
	   }
   }
   
   /**
    * Maximo idcodecorp en esta tabla
    * @return
    */
   public Integer selectMaxidCodeCorp_FromPreCodeCorp(){
	   
	   //select max(idcodecorp) from (SELECT idcodecorp FROM corp union select idcodecorp from poscodecorps union select idcodecorp from precodecorp)
	   String query = "Select max(idcodecorp) as MAXID from PreCodeCorp"
	   def maxid = Database.getInstance().sqlnewcorps.firstRow(query)
	   if ((maxid != null) && (maxid[0] != null))
		   return maxid[0]
	   else return 0
   }
   
   /**
   * Obtiene el corp de idcodecorp
   * @param idcodecorp
   * @return
   */
   public String selectCorp(int idcodecorp) {
	   def corp = Database.getInstance().sqlnewcorps.firstRow ("select corp from corp where idcodecorp = ${idcodecorp}")
	   if (corp != null && corp[0] != null)
		   return corp
	   else return ""
   }
   
   /**
	* Obtiene todos los centros candidatos para el idcodecorp indicado
	* @param idcodecorp
	* @return listado de centros candidatos
	*/
   public List<CandidateCenter> getPreCodeCorpsCenters(int idcodecorp) {
	   
	   List<CandidateCenter> censW = new ArrayList<CandidateCenter>();
	   
	   Database.getInstance().sqlnewcorps.eachRow ("select * from PreCodeCorp where idcodecorp = ${idcodecorp}" , {
		   int id = it[0]
		   String corp = it[1]
		   String iso = it[2]
		   String prov = it[3]
		   String instit = it[4]
		   String org = it[5]
		   String dpto = it[6]
		   String sector = it[7]
		   Center cen = new Center(iso, prov, instit, org, dpto, sector);
		   cen.setIdCe idcodecorp

		   int range = it[8]
		   double permatch = it[9]
		   CandidateCenter cenW = new CandidateCenter();
		   cenW.setCenter cen
		   cenW.set_context_by_range range
		   cenW.set_permatch permatch
		   
		  
		   
		   censW.add(cenW)
	   })
	   
	   return censW;
   }
   
   
   
   /**
   * Elimina de precodecorp los registros que contienen los centros candidatos para el corp indicado por idcodecorp
   * @param idcodecorp idcodecorp a eliminar todos sus registros
   */
   public void deletePreCodeCorps(int idcodecorp) {
	   
		   Database.getInstance().sqlnewcorps.execute("delete * from precodecorp where idcodecorp = ${idcodecorp}");
	   
   }
   
}
